package com.qishi.exampleThree;

import com.qishi.exampleOne.PerformanceMonitor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;

/**
 * 引介增强
 */
public class ControllablePerformanceMonitor extends DelegatingIntroductionInterceptor implements Monitorable {

    private ThreadLocal<Boolean> monitorStatusMap =new ThreadLocal<Boolean>();

    public void setMonitorActive(boolean active) {
        monitorStatusMap.set(active);
    }


    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
        Object object = null;

        if(monitorStatusMap.get() != null && monitorStatusMap.get()) {
            PerformanceMonitor.begin(mi.getClass().getName()+"."+mi.getMethod().getName());
            object = super.invoke(mi);
            PerformanceMonitor.end();
        }else {
            object = super.invoke(mi);
        }

        return object;
    }
}
